<?php
/**
 * This file belongs to huakaili project
 *
 * @author jszhang
 */
global $logid;
//,$uid,$user;
$pageVars = array();
$errmsg = GetMessageStack();
$userDao = GetUserDAO();

//can not use function here as it is the subpage,
//global key word does not work, can not get global vars, shit
//use BLOCK_ENTER and BLOCK_END instead
//
//function ProcessBasicInfo() {
//	global $errmsg, $userDao;
//
//	$params = array();
//	$params['nick'] = trim($_POST['nick']);
//	$params['province'] = trim($_POST['province']);
//	$params['city'] = trim($_POST['city']);
//	$params['introduction'] = trim($_POST['introduction']);
//
//	if( !$userDao->UpdateUserBasicInfo( $params ) ){
//		$errmsg->add( 'basic_info', '写入数据库失败' );
//	}
//}

//write to database
if(isset($_POST['action'])) 
{
	$action=trim($_POST['action']);
	if($action=='detail_info') 
	{
		//use loop to ensure single entrance and single exit
		do
		{
			$params = array();
			$params['id'] = $logid;    //trim($_POST['id'] );  modified by yawei
			
			$params['sex'] = intval($_POST['sex'] );
			if($params['sex']!=0 && $params['sex']!=1)
			{
				$errmsg->add( 'detail_info', '非法的性别信息！', 'error' );
				break;
			}
			
			$year = trim($_POST['biryear']);
			$month = trim($_POST['birmonth']);
			$day = trim($_POST['birday']);
			if($year!='0000'&&($month=='00'||$day=='00'))
			{
				$errmsg->add( 'detail_info', '生日格式不对！', 'error' );
				break;
			}
			else
			{
				$params['birthday'] = $year."-".$month."-".$day." 00:00:00";
			}
			if(date("Y-m-d",strtotime($params['birthday']))<date("Y-m-d",strtotime("1970-01-01"))||
			date("Y-m-d",strtotime($params['birthday']))>=date("Y-m-d",strtotime("2009-01-01")))
			{
    			$errmsg->add( 'detail_info', '不合法的出生日期！', 'error' );
				break;
			}
			
			$params['profession'] = intval($_POST['profession']);
			if($params['profession']<0||$params['profession']>152)
			{
				$errmsg->add( 'detail_info', '不合法的行业/职业！', 'error' );
				break;
			}
			
			$params['education'] = intval($_POST['education']);
			if($params['education']<0 || $params['education']>5)
			{
				$errmsg->add( 'detail_info', '不合法的学历！', 'error' );
				break;
			}
			
			$params['salary'] = intval($_POST['salary']);
			if($params['salary']<0 ||$params['salary']>4)
			{
				$errmsg->add( 'detail_info', '不合法的月收入！', 'error' );
				break;
			}

			$params['qq'] = edp_db_input( trim($_POST['qq']) );
			/*
			if( !edp_is_empty_string($params['qq']) && !preg_match('/^[0-9]{5,10}$/', $params['qq']) )
			{
				$errmsg->add( 'detail_info', 'QQ格式不对！', 'error' );
				break;
			}
			*/
				
			$params['msn'] = edp_db_input( trim($_POST['msn']) );
			/*
			if( (!edp_is_empty_string($params['msn']) &&
				!preg_match('/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/', $params['msn'] ))||strlen($params['msn'])>40 )
			{
				$errmsg->add( 'detail_info', 'MSN格式不对！', 'error' );
				break;
			}
			*/
				
			$params['shopping_method'] = edp_db_input( trim($_POST['shopping_method']) );
			/*
			if(strlen($params['shopping_method'])>'180')
			{
				$errmsg->add( 'detail_info', '购物渠道最大长度不超过180字符！', 'error' );
				break;
			}
			*/
			
			$params['shopping_plat'] = edp_db_input( trim($_POST['shopping_plat']) );
			/*
			if(strlen($params['shopping_plat'])>'180')
			{
				$errmsg->add( 'detail_info', '常用购物平台最大长度不超过180字符！', 'error' );
				break;
			}
			*/
			
			$params['interested_cate'] = edp_db_input( trim($_POST['interested_cate']) );
			/*
			if(strlen($params['interested_cate'])>'180')
			{
				$errmsg->add( 'detail_info', '感兴趣的商品最大长度不超过180字符！', 'error' );
				break;
			}
			*/
			$params['interested_grand'] = edp_db_input( trim($_POST['interested_grand']) );
			/*
			if(strlen($params['interested_grand'])>'180')
			{
				$errmsg->add( 'detail_info', '喜欢的品牌最大长度不超过180字符！', 'error' );
				break;
			}
			*/
//			$params['qq_display'] = trim($_POST['qq_display']);
//			$params['msn_display'] = trim($_POST['msn_display']);
			
			$pageVars = array_merge( $pageVars, $params );
			if( $errmsg->size('detail_info') > 0 )
			{
				break;
			}

			//database operation
			if( !$userDao->UpdateUserDetailInfo($params)){
				$errmsg->add( 'detail_info', '写入数据库失败', 'error' );
			}
			else{
				$errmsg->add( 'detail_info', '更新成功', 'success' );
			}
			
			break;
		}while(1);
	}
	else{
		$errmsg->add( 'detail_info', '未知操作', 'error' );
	}
}
//read from database
//else{
//	$userInfo = getUserFromCookie();
//	if( $userInfo ){
//		//get user basic_info from database

$userDetailInfo = $userDao->GetUserDetailInfo( $logid );
if( $userDetailInfo )
{
	$pageVars['id'] = $userDetailInfo['user_id'];
	$pageVars['sex'] = $userDetailInfo['sex'];
	//$pageVars['province'] = $userDetailInfo['province'];
	$pageVars['birthday'] = $userDetailInfo['birthday'];
	$pageVars['profession'] = $userDetailInfo['profession'];
	$pageVars['education'] = $userDetailInfo['education'];
	$pageVars['salary'] = $userDetailInfo['salary'];
	$pageVars['qq'] = $userDetailInfo['qq'];
	$pageVars['msn'] = $userDetailInfo['msn'];
	$pageVars['shopping_method'] = $userDetailInfo['shopping_method'];
	$pageVars['shopping_plat'] = $userDetailInfo['shopping_plat'];
	$pageVars['interested_cate'] = $userDetailInfo['interested_cate'];
	$pageVars['interested_grand'] = $userDetailInfo['interested_grand'];
//			$pageVars['qq_display'] = $userDetailInfo['qq_display'];
//			$pageVars['msn_display'] = $userDetailInfo['msn_display'];
			
}
else
{
	$errmsg->add( 'detail_info', '用户不存在', 'error' );
}
//	}
//	else{
//		$errmsg->add( 'detail_info', '登录用户信息不合法', 'error' );
//	}
//}
?>
